//
//  VisitorTableViewController.swift
//  Weibo1.0
//
//  Created by lianghuahao on 15/12/9.
//  Copyright © 2015年 lianghuahao.com. All rights reserved.
//

import UIKit

class VisitorTableViewController: UITableViewController {

    /// 用户登录的标记
    private var userLogon = false
    
    //访客视图如果用懒加载会怎样?  - 如果使用懒加载,访客视图始终都会被创建出来,虽然用户看不出来,但是消耗性能,占用内存,涉及内存管理
    var visitorView: VisitorView?
//    lazy var visitorView: VisitorView? = VisitorView()
    
    override func loadView() {
        //根据用户登陆情况, 决定显示的根视图
        userLogon ? super.loadView() : setupVisitorView()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        print(visitorView)
    }
    
    private func setupVisitorView() {
        //替换根视图
        visitorView = VisitorView()
        //设置代理
//        visitorView?.delegate = self
        
        view = visitorView
        //添加监听方法
        visitorView?.registerButton.addTarget(self, action: "visitorViewDidRegister", forControlEvents: UIControlEvents.TouchUpInside)
        visitorView?.loginButton.addTarget(self, action: "visitorViewDidLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        //修改导航栏全局外观, 必须在创建控件之前设置, 一经设置全局有效(所以这不行)
//        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "visitorViewDidRegister")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: UIBarButtonItemStyle.Plain, target: self, action: "visitorViewDidLogin")
    }
    
}
// MARK: - 访客视图监听方法
extension VisitorTableViewController  {
    
    func visitorViewDidRegister() {
        print("注册")
    }
    func visitorViewDidLogin() {
        print("登陆")
    }
}